<template>
  <div class="app-container">

    <el-table v-loading="loading" :data="inviteList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="邀请人ID" align="center" prop="fromId" />
      <el-table-column label="队伍名称" align="center" prop="teamName" >
        <template #default="scope">
          <el-link type="primary" @click="getTeamInfoVo(scope.row)">
            {{ scope.row.teamName }}
          </el-link>
        </template>
      </el-table-column>
      <el-table-column label="受邀者名称" align="center" prop="toName">
        <template #default="scope">
          <el-link type="primary" @click="openUserInfoDialog(scope.row.toName)">
            {{ scope.row.toName }}
          </el-link>
        </template>
      </el-table-column>
      <el-table-column label="邀请语" align="center" prop="tips" />
      <el-table-column label="邀请状态" align="center" prop="inviteStatus">
        <template #default="scope">
          <dict-tag :options="invite_status" :value="scope.row.inviteStatus"/>
        </template>
      </el-table-column>

    </el-table>

    <pagination
        v-show="total > 0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />

<!--    队伍详情弹窗-->
    <el-dialog :title="队伍详情" v-model="TeamInfoOpen" width="1000px" append-to-body class="custom-dialog">
      <el-row>
        <el-col :span="12">队伍ID：{{form.teamId}}</el-col>
        <el-col :span="12">队伍名称：{{form.teamName}}</el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          队长名称：
          <el-link type="primary" @click="openUserInfoDialog(form.leaderName)">
            {{ form.leaderName }}
          </el-link>
        </el-col>

        <el-col :span="24">队伍简介：{{form.teamOutline}}</el-col>
      </el-row>
      <el-row>
        <el-col :span="12">活动ID：{{form.actId}}</el-col>
        <el-col :span="12">活动名称：{{form.actTitle}}</el-col>
      </el-row>
      <!-- 队员信息 -->
      <el-row>
        <el-col :span="24"><b>队员名单：</b></el-col>
      </el-row>
      <el-row v-for="(row, index) in formattedMembers" :key="index">
        <el-col :span="12" v-for="(member, idx) in row" :key="idx">
          <el-link type="primary" @click="openUserInfoDialog(member)">
            {{ member }}
          </el-link>
        </el-col>
      </el-row>
    </el-dialog>

    <!--用户详细信息弹窗-->
    <el-dialog
        :title="userInfo.nickName + ' 的详细信息'"
        v-model="userInfoDialogVisible"
        width="500px"
        append-to-body
    >
      <el-descriptions column="1" border>
        <el-descriptions-item label="昵称">{{ userInfo.nickName }}</el-descriptions-item>
        <el-descriptions-item label="手机号">{{ userInfo.phoneNumber }}</el-descriptions-item>
        <el-descriptions-item label="邮箱">{{ userInfo.email }}</el-descriptions-item>
        <el-descriptions-item label="性别">{{ userInfo.sex === '0' ? '男' : userInfo.sex === '1' ? '女' : '未知' }}</el-descriptions-item>
      </el-descriptions>


      <div style="margin-top: 20px;">
        <b>用户标签：</b>
        <div style="margin-top: 10px;">
          <el-tag
              v-for="(tag, index) in userTags"
              :key="index"
              type="success"
              style="margin-right: 8px;"
          >
            {{ tag.tagName }}
          </el-tag>
        </div>
      </div>
    </el-dialog>

  </div>
</template>

<style scoped>
.custom-dialog {
  border-radius: 12px;
  box-shadow: 0 6px 12px rgba(0, 0, 0, 0.1);
  background-color: #fff;
  width: 600px; /* 增大对话框宽度 */
}

.custom-dialog .el-dialog__header {
  background-color: #f0f9eb;
  border-bottom: 1px solid #ebedf0;
  padding: 20px; /* 增加头部的内边距 */
}

.custom-dialog .el-dialog__title {
  color: #67c23a;
  font-size: 22px; /* 增大标题字体大小 */
  font-weight: bold; /* 标题加粗 */
}

.custom-dialog .el-row {
  margin-bottom: 20px; /* 增大行之间的间距 */
}

.custom-dialog .el-col {
  color: #333;
  font-size: 16px; /* 增大内容字体大小 */
}

.custom-dialog .el-col b {
  color: #606266;
  font-size: 18px; /* 增大加粗文本的字体 */
}

.custom-dialog .el-col .el-row {
  margin-bottom: 10px;
}

</style>
<script setup name="Invite">
import { listInvite, getInvite, delInvite, addInvite, updateInvite, myInviteList} from "@/api/match/invite";
import { getUserInfo } from "@/api/match/UserInfo";
import { getTag } from "@/api/match/MyTag";
import { listTeams, getTeams, delTeams, addTeams, updateTeams, showJoinedTeams} from "@/api/match/teams";

const { proxy } = getCurrentInstance();
const { invite_status } = proxy.useDict('invite_status');

const inviteList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    teamId: null,
    inviteStatus: null
  },
  rules: {
    inviteStatus: [
      {required: true, message: "邀请状态不能为空", trigger: "change"}
    ]
  }
});

const {queryParams, form, rules} = toRefs(data);




/** 查询邀请列表 */
function getList() {
  loading.value = true;
  myInviteList().then(response => {
    inviteList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

/** 查看队伍详情按钮操作 */
const TeamInfoOpen = ref(false)
function getTeamInfoVo(row) {
  reset();
  const _teamId = row.teamId
  getTeams(_teamId).then(response => {
    form.value = response.data;
    TeamInfoOpen.value = true;
  });
}


// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    fromId: null,
    toId: null,
    teamId: null,
    tips: null,
    inviteStatus: null
  };
  proxy.resetForm("inviteRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}
// 用户信息相关方法
const userInfoDialogVisible = ref(false)
const userInfo = reactive({
  nickName: '',
  phoneNumber: '',
  email: '',
  sex: ''
})
const userTags = ref([])
// 点击用户名称时调用
const openUserInfoDialog = async (userName) => {
  const res1 = await getUserInfo(userName)
  Object.assign(userInfo, res1.data || {})

  getTag(userName).then(response => {
    userTags.value = response.rows || [];
  });

  userInfoDialogVisible.value = true
}


/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.fromId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加邀请";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _fromId = row.fromId || ids.value;
  getInvite(_fromId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改邀请";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["inviteRef"].validate(valid => {
    if (valid) {
      if (form.value.fromId != null) {
        updateInvite(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addInvite(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _fromIds = row.fromId || ids.value;
  proxy.$modal.confirm('是否确认删除邀请编号为"' + _fromIds + '"的数据项？').then(function () {
    return delInvite(_fromIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {
  });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('match/invite/export', {
    ...queryParams.value
  }, `invite_${new Date().getTime()}.xlsx`)
}

getList();




import { computed } from "vue";
// import {getConfirmedNotices} from "../../../api/match/confirm.js";

const formattedMembers = computed(() => {
  if (!form.value.memberNames) return [];
  const members = form.value.memberNames.split(", "); // 按逗号分割
  const result = [];
  for (let i = 0; i < members.length; i += 2) {
    result.push(members.slice(i, i + 2)); // 每两个一组
  }
  return result;
});
</script>

